define(function (require) {
    var Base = require('common/base/object');
    var Mask = require('common/mask');
    var mask = new Mask();

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(_this, [params]);
            _this.settings = {
                //容器
                container: '#J_waterfall',
                //加载条
                loading_bar: '#J_wall_loading',
                //请求地址
                ajax_url: null,
                ajax_params: {},
                //高度微调
                distance: 50,
                spage: 2,
                content_html: null,
                auto_load: true
            };
            _this.init(params);
        },
        init: function (options) {
            var _this = this;
            if (options) {
                if (_.empty(options.distance)) {
                    options.distance = _global.wall_distance;
                }
                if (_.empty(options.max_spage)) {
                    options.max_spage = _global.wall_spage_max;
                }

                $.extend(_this.settings, options);
            }
            var s = _this.settings;
            if (s.ajax_url == null) {
                s.ajax_url = $(s.container).attr('data-uri');
            }
            var distance = $(s.container).attr('data-distance');
            if (distance) {
                s.distance = distance;
            }
            _this.is_loading = false;

            $(window).bind('scroll', function () {
                if (s.auto_load) {
                    _this.lazy_load();
                }
            });
            _this.is_fetch_all = false;
        },
        start: function (params) {
            var _this = this;
            _this.settings.auto_load = true;
            if (params) {
                if (params.spage <= 1) {
                    $(_this.settings.container).html('');
                }
                _this.settings = _.extend(_this.settings, params);
            }
            _this.is_fetch_all = false;
            _this.loader();
        },
        stop: function () {
            var _this = this;
            _this.settings.auto_load = false;
        },
        //加载
        lazy_load: function () {
            var _this = this;
            var s = _this.settings,
                st = $(document).height() - $(window).scrollTop() - $(window).height();
            if (!_this.is_loading && $(s.loading_bar)[0] && st <= s.distance) {
                _this.loader();
            }
        },
        //加载状态
        is_loading: true,
        //执行加载
        loader: function () {
            var _this = this;
            var s = _this.settings;
            if (_this.is_fetch_all) {
                return;
            }
            _this.is_loading = true;
            $(s.loading_bar).show();
            $.ajax({
                url: s.ajax_url,
                data: _.extend(s.ajax_params, {p: s.spage}),
                type: 'GET',
                dataType: 'json',
                success: function (result) {
                    if (result.status == 1) {
                        var data = result.data ? result.data : result.data.data;
                        var html = s.content_html == null ? data.html : s.content_html(result);

                        $(s.container).append(html);
                        $(s.loading_bar).hide(); //隐藏加载条
                        _this.is_loading = false; //可以继续加载
                        $('#page-num').html(s.spage);

                        if (data.isfull == 0) {
                            _this.is_fetch_all = true;
                            if (s.spage == 1 && s.content_html && result.data.list.length == 0) {
                                $(s.container + ' .waterfall-no-data').remove();
                                $(s.container).append(_this.render(require('text!./templates/waterfall_no_data.html'), {}));
                            }
                        }
                        else {
                            s.spage++;
                        }
                        if (s.fn) {
                            s.fn();
                        }
                    }
                    else {
                        mask.error(result.msg);
                    }
                }
            });
        }
    });
});
